﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata.StateTracker;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core.Const;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.BarElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args.UnitTest;
using Kingdee.BOS.Contracts;
using System.Threading.Tasks;
using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;

namespace Sincetech.K3.SCM.Purchase
{
    /// <summary>
    /// 采购订单：单据插件
    /// </summary>
    public class PurchaseOrder_Bill : AbstractDynamicFormPlugIn
    {
        public override void OnInitialize(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.InitializeEventArgs e)
        {
            base.OnInitialize(e);
        }

        string strValue = "";
        /// <summary>
        /// 值更新事件
        /// </summary>
        /// <param name="e"></param>
        public override void DataChanged(DataChangedEventArgs e)
        {

            base.DataChanged(e);
            //objs
            //var a = metadata.BusinessInfo.GetField("FMaterialId");
            switch (e.Field.Key.ToString())
            {
                //case "FNAME":
                //case "FORGSUBJECTSCHEMAID":          //组织隶属方案
                case "FMaterialId":                //物料编码
                    //组织隶属方案和上级部门变化，重新生成部门全称
                    //其中BuildNewFullName为拼接全称函数，此处省略具体实现逻辑
                    //this.Model.SetValue("FFullName", BuildNewFullName(e.Key));
                    //this.View.UpdateView("FFullName");
                    //获取元数据服务
                    IMetaDataService metadataService = Kingdee.BOS.App.ServiceHelper.GetService<IMetaDataService>();
                    //获取物料单据元数据
                    //FormMetadata materialMetadata = metadataService.Load(this.Context,"BD_Material") as FormMetadata;
                    FormMetadata materialMetadata = (FormMetadata)Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(this.View.Context, "BD_MATERIAL");
                    //获取保存服务
                    //ISaveService saveService = Kingdee.BOS.App.ServiceHelper.GetService<ISaveService>();
                    //获取加载数据服务
                    IViewService viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();

                    DynamicObject material = (DynamicObject)this.Model.GetValue(e.Field);
                    string materialId = Convert.ToString(material["Id"]);

                    //DynamicObject[] objs = viewService.Load(
                    //    this.Context,
                    //    new object[] { materialId },
                    //    materialMetadata.BusinessInfo.GetDynamicObjectType());
                    //    strValue = Convert.ToString(objs[0]["Id"]);

                    //string sql = string.Format(@"SELECT F_ST_USEPAIRQTYFLAG FROM T_BD_MATERIAL WHERE FMATERIALID = {0}))", material["Id"]);
                    //using (IDataReader dr = DBUtils.ExecuteReader(this.Context, sql))
                    //{
                    //    dr["F_ST_USEPAIRQTYFLAG"].ToString();

                    //}
                    break;
            }

        }

        /// <summary>
        /// 单元格获取焦点事件
        /// </summary>
        /// <param name="e"></param>
        public override void EntryCellFocued(EntryCellFocuedEventArgs e)
        {
            base.EntryCellFocued(e);

            if (e.NewFieldKey.ToUpper() == "F_ST_QTY") 
            {
                BillShowParameter showParam = new BillShowParameter();
                //单据名，不能使用继承的单据名，原因未知
                showParam.FormId = "567d468c-e088-44f0-80a0-5e18efea79ee";
                //该单据的主键值，新建时可以不填
                //showParam.PKey = "100001";
                showParam.Status = OperationStatus.ADDNEW;
                showParam.ParentPageId = this.View.PageId;
                showParam.OpenStyle.ShowType = ShowType.Floating;
                //传递特殊属性到目标模型
                //目标模型可以在其表单插件中的OnInitial事件中获取这些参数
                //传递物料编码
                showParam.CustomParams.Add("FMATERIALID", strValue);
                showParam.CustomComplexParams.Add("parentView", this.View);

                this.View.ShowForm(showParam);

                //显示id为10001的物料，关闭此物料界面时，处理一些回调逻辑，接收弹出的子窗体返回的数据
                this.View.ShowForm(showParam,
                    (result) =>
                    {
                        object returndata = result.ReturnData;
                        //对返回的数据做业务处理
                    }
                );
            }
        }

        /// <summary>
        /// 单据体行单击事件，单击单据体行时触发
        /// </summary>
        /// <param name="e"></param>
        public override void EntityRowClick(EntityRowClickEventArgs e)
        {
 	         base.EntityRowClick(e);
        }

        /// <summary>
        /// 单据体行双击事件，双击单据体行时触发
        /// </summary>
        /// <param name="e"></param>
        public override void EntityRowDoubleClick(EntityRowClickEventArgs e)
        {
            //获取物料内码信息
            BusinessInfo info = this.View.OpenParameter.FormMetaData.BusinessInfo;
            Entity subEntity = info.GetEntity("FSubEntity");
            Entity parEntity = info.GetEntity("FPOOrderEntry");

            DynamicObject objMaterial = this.Model.GetValue("FMaterialID", e.Row) as DynamicObject;


            //DynamicObject material = this.Model.GetEntityDataObject( this.Model.GetEntryCurrentFieldKey,e.Row);
            //string materialId = Convert.ToString(material["Id"]);

            //DynamicObject[] objs = viewService.Load(
            //    this.Context,
            //    new object[] { materialId },
            //    materialMetadata.BusinessInfo.GetDynamicObjectType());
            //    strValue = Convert.ToString(objs[0]["Id"]);

            //string sql = string.Format(@"SELECT F_ST_USEPAIRQTYFLAG FROM T_BD_MATERIAL WHERE FMATERIALID = {0}))", material["Id"]);
            //using (IDataReader dr = DBUtils.ExecuteReader(this.Context, sql))
            //{
            //    dr["F_ST_USEPAIRQTYFLAG"].ToString();

            //}
            if (e.ColKey.ToUpper() == "F_ST_QTY")
            {
                BillShowParameter showParam = new BillShowParameter();
                //单据名，不能使用继承的单据名，原因未知
                showParam.FormId = "567d468c-e088-44f0-80a0-5e18efea79ee";
                //该单据的主键值，新建时可以不填
                //showParam.PKey = "100001";
                showParam.Status = OperationStatus.ADDNEW;
                showParam.ParentPageId = this.View.PageId;
                showParam.OpenStyle.ShowType = ShowType.Floating ;
                //showParam.OpenStyle.ShowType = ShowType.InContainer;

                //传递特殊属性到目标模型
                //目标模型可以在其表单插件中的OnInitial事件中获取这些参数
                //传递物料编码
                showParam.CustomParams.Add("FMATERIALID", strValue);
                showParam.CustomComplexParams.Add("parentView", this.View);

                this.View.ShowForm(showParam);

                //显示id为10001的物料，关闭此物料界面时，处理一些回调逻辑，接收弹出的子窗体返回的数据
                this.View.ShowForm(showParam,
                    (result) =>
                    {
                        object returndata = result.ReturnData;
                        //对返回的数据做业务处理
                    }
                );
            }
        }
    }
}
